using UnityEngine;
using System.Collections;

public class SwitchSceneManager : MonoBehaviour {
	
	#region Singleton
    // Instance de la classe
    private static SwitchSceneManager _instance;

    /// Propriété pour accéder publiquement à l'instance de la classe
    public static SwitchSceneManager Instance
    {
        get
        {
            // Si l'instance n'existe pas, on la crée
            if (SwitchSceneManager._instance == null)
            {
                SwitchSceneManager._instance = new SwitchSceneManager();
            }
            return SwitchSceneManager._instance;
        }
    }
    #endregion
	
	private AnimationClip _fadeOut;
	private AnimationClip _fadeIn;
	
	private float _timeFadeOut = 2.0f;
	private float _timeFadeIn = 1.5f;
	
	private GameObject _blackPlane;
	
	void Awake () 
    {
		// Unity créera l'objet même si le constructeur est privé donc on doit initialiser l'instance de notre singleton ici
        if (SwitchSceneManager._instance == null) {
            SwitchSceneManager._instance = this;
        } else if (SwitchSceneManager._instance != this) {
            Destroy(this.gameObject);
        }

	
		//Black Texture Creation
		Texture2D _blackTexture = new Texture2D(1, 1);
		_blackTexture.SetPixel(0, 0, Color.black);
		_blackTexture.Apply();
		
		//Create Plane and position child of camera
		_blackPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
		_blackPlane.renderer.material.shader = Shader.Find("Transparent/Diffuse");
		_blackPlane.renderer.material.SetTexture("_MainTex", _blackTexture);
		_blackPlane.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z + 1);
		_blackPlane.transform.parent = this.transform;
		_blackPlane.transform.localScale = new Vector3(100, 100, 100);
		_blackPlane.transform.Rotate(-90, 0, 0);
		_blackPlane.AddComponent(typeof(Animation));
		
		//Launch Fade Out
		FadeOut();
		
	}
	
	void FadeOut()
	{
		//Fade Out animation creation
		if(_fadeOut == null)
		{
			_fadeOut = new AnimationClip();
			AnimationCurve curveAlpha = new AnimationCurve();
			
			curveAlpha.AddKey(0, 1);
			curveAlpha.AddKey(_timeFadeOut, 0);
			
			_fadeOut.SetCurve("", typeof(Material), "_Color.a", curveAlpha);
			_blackPlane.animation.AddClip(_fadeOut, "Fade Out");
		}
		
		_blackPlane.animation.Play("Fade Out");
	}
	
	void FadeIn()
	{
        //Fade In animation creation
        if (_fadeIn == null)
        {
            _fadeIn = new AnimationClip();
            AnimationCurve curveAlpha = new AnimationCurve();

            curveAlpha.AddKey(0, 0);
            curveAlpha.AddKey(_timeFadeIn, 1);

            _fadeIn.SetCurve("", typeof(Material), "_Color.a", curveAlpha);
            _blackPlane.animation.AddClip(_fadeIn, "Fade In");
        }

        _blackPlane.animation.Play("Fade In");
	}
	
	public void SwitchToScene(string nameScene)
	{
		FadeIn();
		StartCoroutine(WaitForSceneLaunch(nameScene));
	}
	
	IEnumerator WaitForSceneLaunch(string name)
	{
		yield return new WaitForSeconds(_timeFadeIn);
		Application.LoadLevel(name);
	}
}
